package org.alg.graph;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-5-7
 * Time: 上午12:01
 * To change this template use File | Settings | File Templates.
 */
public class SymbolGraph {
    private Map<String,Integer> map;
    private String[] keys;
    private Graph G;

    public SymbolGraph(String stream,String sp) throws Exception{
        map=new HashMap<String,Integer>();
        BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(stream))));
        String s;
        int cnt=0;
        while((s=reader.readLine())!=null){
            String[] a=s.split(sp);
            cnt+=a.length;
            for(int i=0;i<a.length;i++){
                if(!map.containsKey(a[i]))
                    map.put(a[i],map.size());
            }
        }
        reader.close();

        System.out.println("graph node count:"+cnt);
        G=new Graph(map.size());
        keys=new String[map.size()];

        for(String name:map.keySet()){
            keys[map.get(name)]=name;
        }

        reader=new BufferedReader(new InputStreamReader(new FileInputStream(new File(stream))));
        while((s=reader.readLine())!=null){
            String[] a=s.split(sp);
            int v=map.get(a[0]);
            for(int i=1;i<a.length;i++)
                G.addEdge(v,map.get(a[i]));
        }
    }

    public boolean constains(String s){
        return map.containsKey(s);
    }

    public int index(String s){
        return map.get(s);
    }

    public String name(int i){
        return keys[i];
    }

    public Graph G(){
        return G;
    }

    public static void main(String[] args){

    }

}
